<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/10/25
 * Time: 下午2:09
 */

namespace app\admin\controller;


use think\Controller;

class Log   extends Controller
{
    public function login ()
    {
        if (!is_null(session('name'))) {
            $this->redirect('member/index' );
        }
        return view();
    }

    public function checklogin ()
    {
        $post['username'] = input('post.name' , '' , 'htmlspecialchars,trim');
        $post['password'] = input('post.pwd' , '' , 'trim,md5');

        $res = db('admin')->where($post)->find();
        if (null == $res)
        {
            $this->error('用户名或密码错误');
        }

        session('admin_id' , $res['id']);
        session('tuijian_id' , $res['id2']);
        session('admin_nickname' , $res['nickname']);
        session('admin_headimg' , $res['header_img']);

        // 获取用户角色
        $admin_role = model('adminRole');
        $role_id = $admin_role->getRoleIds(session('admin_id'));
        session('role_ids' , $role_id);

        // 获取角色权限
        $rolePermission = model('rolePermission');
        $permission_id = $rolePermission->getPermissions($role_id);
        session('permission_ids' , $permission_id);

        $this->success('登录成功' , url('member/index'));

    }

    public function logout (){
        // 销毁session
        session('name' , null);

        $this->success('退出成功' , url('login'));
    }
}